package com.galleryviewpager.i_wave_loading;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class WaveLoadingView3 extends View {
    private static final String TAG = "WaveLoadingView2";

    // TODO: 2019/9/16 具体关系对应本包下面的paint_mode.png
    private PorterDuffXfermode mMode = new PorterDuffXfermode(PorterDuff.Mode.SRC_IN);

    private int mWidth, mHeight;

    private Canvas mCanvas;
    private Bitmap mBitmap;
    private Path mPath = new Path();
    private Paint mPaint = new Paint();

    private int x =60, y;
    private boolean isLeft;
    private Paint mSRCPaint;

    public WaveLoadingView3(Context context) {
        this(context, null);
    }

    public WaveLoadingView3(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveLoadingView3(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {

        mPaint.setStrokeWidth(10f);
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.parseColor("#8800ff66"));

        mSRCPaint = new Paint();
        mSRCPaint.setAntiAlias(true);
        mSRCPaint.setColor(Color.parseColor("#88dddddd"));

        mBitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = widthSize;
        }

        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = heightSize;
        }
        setMeasuredDimension(mWidth, mHeight);

        y = mHeight;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        Log.e(TAG, "onDraw: x=> "+x+" y=> "+y );

        if (x > 50) {
            isLeft = true;
        } else if (x < 0) {
            isLeft = false;
        }
        if (y > -50) {
            y--;
        }
        if (isLeft) {
            x-=1;
        } else {
            x+=1;
        }
        mPath.reset();
        mPath.moveTo(0, y);
        mPath.cubicTo(100 + x * 2, 50 + y, 100 + x * 2, y - 50, mWidth, y);
        mPath.lineTo(mWidth, mHeight);
        mPath.lineTo(0, mHeight);
        mPath.close();

        mBitmap.eraseColor(Color.parseColor("#00000000"));

        mCanvas.drawCircle(mWidth/2,mHeight/2,mWidth/2,mSRCPaint);

        mPaint.setXfermode(mMode);
        mCanvas.drawPath(mPath, mPaint);
        mPaint.setXfermode(null);
        canvas.drawBitmap(mBitmap, 0, 0, null);
        postInvalidateDelayed(10);




////        if (y > -50) {
////            y--;
////        }
//        if (x > 50) {
//            isLeft = true;
//        } else if (x < 0) {
//            isLeft = false;
//        }
//
//        if (isLeft) {
//            x = x - 1;
//        } else {
//            x = x + 1;
//        }
//        mPath.reset();
//        mPath.moveTo(0, y);
//        mPath.cubicTo(100 + x * 2, 50 + y, 100 + x * 2, y - 50, mWidth, y);
//        mPath.lineTo(mWidth, mHeight);
//        mPath.lineTo(0, mHeight);
//        mPath.close();
//
//
//        //清除掉图像 不然path会重叠
//        mBitmap.eraseColor(Color.parseColor("#00000000"));
//        //dst
////        mCanvas.drawBitmap(bgBitmap,0,0,null);
//
////        mSRCPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
//
//        mCanvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2, mSRCPaint);
//
//        mPaint.setXfermode(mMode);
//        //src
//        mCanvas.drawPath(mPath, mPaint);
//        mPaint.setXfermode(null);
//
//
//        canvas.drawBitmap(mBitmap, 0, 0, null);
//
//
//        postInvalidateDelayed(10);


    }
}
